package mine.activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.orm.SugarRecord;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import java.util.ArrayList;
import java.util.List;

import mine.adapter.BeiWangAdapter;
import mine.broadcast.AlarmReceiver;
import model.BeiWang;

/**
 * 备忘录
 * Created by Administrator on 2016/3/19.
 */
public class BeiWangActivity extends UIHandler.BaseActivity {

    ListView lv_beiwang;
    List<BeiWang> beiWangs = new ArrayList<BeiWang>();
    BeiWangAdapter beiWangAdapter = new BeiWangAdapter();
    {
        beiWangAdapter.setBeiWangs(beiWangs);
    }
    Button btn_add;


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_add)
            {
                Intent intent = new Intent(BeiWangActivity.this,AddBeiWangActivity.class);
                BeiWangActivity.this.startActivity(intent);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beiwang);
        initview();
    }

    @Override
    protected void onStart() {
        super.onStart();
        initdata();
    }

    private void initdata() {
        List<BeiWang> beiWangsTmp = SugarRecord.findWithQuery(BeiWang.class,"select * from BEIWANG order by time desc");
        beiWangs.clear();
        beiWangs.addAll(beiWangsTmp);
        beiWangAdapter.notifyDataSetChanged();
    }

    private void initview() {
        btn_add = (Button) findViewById(R.id.id_btn_add);
        btn_add.setOnClickListener(onClickListener);
        lv_beiwang = (ListView) findViewById(R.id.id_lv_beiwang);
        lv_beiwang.setAdapter(beiWangAdapter);

        lv_beiwang.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                BeiWang beiWang = beiWangs.get(position);
                SugarRecord.delete(beiWang);
                beiWangs.remove(position);
                beiWangAdapter.notifyDataSetChanged();

                AlarmManager mAlarm = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
                Intent intent = new Intent(BeiWangActivity.this,
                        AlarmReceiver.class);
                intent.putExtra("id", beiWang.getId());
                PendingIntent pendingIntent = PendingIntent.getBroadcast(
                        BeiWangActivity.this, beiWang.getId().intValue(), intent, 0);
                mAlarm.cancel(pendingIntent);

                return false;
            }
        });
    }
}
